package com.yuyou.train.util.tool;

import java.util.Random;

public class GenerateVerCodeUtils {

	

	/**
	 * 产生随机的N位数 
	 * @param leng 4<=leng<=9
	 * @return
	 */
    public static String generateVerCode(int leng){  
    	if(leng<4 || leng>9){
    		return null;
    	}
    	//元素

    	int[] array = {1,2,3,4,5,6,7,8,9};

    	//随机对象
    	Random rand = new Random();

    	//循环产生
    	for (int i = 9; i > 1; i--) {
    	int index = rand.nextInt(i);
    	int tmp = array[index];
    	array[index] = array[i - 1];
    	array[i - 1] = tmp;
    	}

    	//拼接结果为字符串
    	int result = 0;
    	for(int i = 0; i < leng; i++)
    	result = result * 10 + array[i];
    	String sixString = Integer.toString(result);

    	//有可能出现5位数，前面加0补全
    	if (sixString.length() == leng-1) {
    	sixString = "0" + sixString;
    	}
    	//打印结果

    	System.out.println(sixString);
    	
    	return sixString;
    }
    
    public static void main(String[] args) {
    	for(int i=0;i<10000;i++){
        	generateVerCode(4);
    	}

	}
    
	
}
